import { _decorator, Component, Node, Button, instantiate, resources, ScrollView } from 'cc';
const { ccclass, property } = _decorator;

// 引入 MessageBoxManager 脚本
import { MessageBoxManager } from './MessageBoxManager';
import { BackpackManager } from './BackpackManager';


@ccclass('OpenBox')
export class OpenBox extends Component {
    @property(Button)
    openButton: Button[] = [];

    @property(MessageBoxManager)
    messageBoxManager: MessageBoxManager = null;

    //背包管理组件
    @property(BackpackManager)
    backpackManager: BackpackManager = null;

    //当前打开的
    curOpenNode:Node=null;

    //地图滚动视图
    @property(ScrollView)
    mapScrollViewNode:ScrollView=null;

    start() {
        this.openButton.forEach(button => {
            button.node.on(Button.EventType.CLICK, this.onstartUIBtnClick, this);
        })
    }

    update(deltaTime: number) {

    }


    private onstartUIBtnClick(button: Button) {
        const buttonName = button.node.name;
        if (buttonName === '打开系统') {
            if(this.curOpenNode!==null){
                this.curOpenNode.active=false;
            }
            this.curOpenNode=null;
            console.log("打开系统");
        } else if (buttonName === '查看状态') {
            if(this.curOpenNode!==null){
                this.curOpenNode.active=false;
            }
            this.curOpenNode=null;
            console.log("查看状态");
        } else if (buttonName === '打开场景') {
            if(this.curOpenNode!==null){
                this.curOpenNode.active=false;
            }
            this.curOpenNode=null;
            console.log("打开场景");

        } else if (buttonName === '开启背包') {
            if(this.curOpenNode!==null&&this.backpackManager.node.active===false){
                this.curOpenNode.active=false;
            }
            //激活背包
            this.backpackManager.node.active = !this.backpackManager.node.active;
            this.curOpenNode=this.backpackManager.node;
            if (this.backpackManager.node.active) {
                this.backpackManager.updateUI();
                // 在消息框中添加消息
                this.messageBoxManager.showMessage(0, '玩家打开背包');
            }
            this.curOpenNode=this.backpackManager.node;
            console.log("打开背包");

        } else if (buttonName === '打开地图') {
            if(this.curOpenNode!==null&&this.mapScrollViewNode.node.active===false){
                this.curOpenNode.active=false;
            }
            this.mapScrollViewNode.node.active=!this.mapScrollViewNode.node.active;
            // miniMap.active=!miniMap.active;
            this.curOpenNode=this.mapScrollViewNode.node;
            if (this.mapScrollViewNode.node.active) {
                this.messageBoxManager.showMessage(0, '玩家看了看地图');
            console.log("打开地图");
            }else{
                this.messageBoxManager.showMessage(0, '地图藏起来了');
                console.log("关闭地图");
            }
        }
    }
}


